import logging

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

from common.doLog import DoLog
from pages.search_page import SearchPage, ge, wd

sp = SearchPage()
dolog = DoLog().set_get_logger(logging.INFO)


class ProductPage():
    # 输入数量
    input = (By.ID, 'number')
    # 加入按钮
    button = (By.LINK_TEXT, '加入购物车')

    def productPage(self, txt, num):
        try:
            sp.searchPage(txt)
        except Exception as e:
            dolog.error(f"发生异常，异常是{e}")
        else:
            dolog.info("运行成功")
        finally:
            dolog.info("运行完毕")

        try:
            ge.getElement(wd, *self.input).send_keys(Keys.BACK_SPACE)
        except Exception as e:
            dolog.error(f"发生异常，异常是{e}")
        else:
            dolog.info("运行成功")
        finally:
            dolog.info("运行完毕")

        try:
            ge.getElement(wd, *self.input).send_keys(num)
        except Exception as e:
            dolog.error(f"发生异常，异常是{e}")
        else:
            dolog.info("运行成功")
        finally:
            dolog.info("运行完毕")

        try:
            ge.getElement(wd, *self.button).click()
        except Exception as e:
            dolog.error(f"发生异常，异常是{e}")
        else:
            dolog.info("运行成功")
        finally:
            dolog.info("运行完毕")


if __name__ == '__main__':
    pp = ProductPage()
    pp.productPage('钻石', 5)
